<template>
  <div class="login">
    <!-- card组件 -->
    <el-card class="card">
      <div class="img">
        <img src="../assets/image/logo.png" />
      </div>
      <!-- 表单 -->
      <!-- :rules表单校验规则 -->
      <el-form
        class="login-form"
        :rules="rules"
        :model="loginForm"
        ref="loginFormRef"
      >
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" show-password>
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <div class="clearfix">
          <el-button type="primary" class="button-login" @click="handleLogin"
            >登录</el-button
          >
          <el-button type="info" class="button-reset" @click="handleReset"
            >重置</el-button
          >
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入您的账号", trigger: "blur" },
          {
            min: 3,
            max: 18,
            message: "账号长度在 3 到 18 个字符",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入您的密码", trigger: "blur" },
          {
            min: 6,
            max: 18,
            message: "密码长度在 6 到 18 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // // 重置按钮功能
    handleReset() {
      this.loginForm.username = "";
      this.loginForm.password = "";
    },
    //登录按钮功能
    async handleLogin() {
      let { data: res } = await this.$http.post("login", this.loginForm);
      window.sessionStorage.setItem("token", res.data.token);
      this.$router.push("/home");
      // 提示
      return this.$message.success(res.meta.msg);
    }
    // handleReset() {
    //   this.loginForm.username = "";
    //   this.loginForm.password = "";
    // },
    // handleLogin(){
    //   this.$refs.loginFormRef.validate(async valid =>{
    //     if(!valid) return false;
    //     let {data:res} = await this.$http.post("login",this.loginForm);
    //     if(res.meta.status ==400){
    //       window.sessionStorage.removeItem("token");
    //       return this.$message.error(res.meta.msg);
    //     }
    //     if(res.meta.status ==200){
    //       window.sessionStorage.setItem("token",res.data.token);
    //       this.$router.push("/home");
    //       return this.$message.success(res.meta.msg);
    //     }
    //   })
    // }
  }
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: aquamarine;
}
.card {
  width: 450px;
  height: 304px;
  background-color: darkslateblue;
  border-radius: 4px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  overflow: initial;
}
.img {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 8px;
  box-shadow: 0 0 10px #eee;
  position: absolute;
  background-color: #fff;
  left: 50%;
  transform: translate(-50%, -60%);
  overflow: hidden;
}

img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.login-form {
  margin-top: 100px;
}
.button-reset {
  float: right;
}
</style>
